package April._26.homework.work4;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AuthorizationTest {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
        executeMethod("deleteData","root");
    }

    public static void executeMethod(String methodName,String userRole) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
        //获取字节码文件
        Class<?> aClass = Class.forName("April._26.homework.work4.Services");
        //获取方法
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if(declaredMethod.equals(methodName)){
                boolean annotationPresent = declaredMethod.isAnnotationPresent(Authorize.class);
                if(annotationPresent){
                    Authorize annotation = declaredMethod.getAnnotation(Authorize.class);
                    String role = annotation.role();
                    //获取对象实例
                    Object o = aClass.newInstance();
                    if(role.equals(userRole)){
                        declaredMethod.invoke(o);
                    }
                }
            }
        }
    }
}
